const JWT = require("../utils/jwtUtil");

const regToken = /Bearer (.+)/;
const loginCheck = (req, res, next) => {
  /* 提取token */
  let token = req.headers["authorization"];
  token = regToken.exec(token)[1];

  const payload = JWT.verify(token);

  if (payload) {
    /* token校验通过直接放行=>请求处理权移交下一个中间件 */
    next();
  } else {
    /* 否则返回请先登录 */
    res.json({ msg: "请先登录" });
  }
};

module.exports = loginCheck
